package first.endtoend

/**
 * Created with IntelliJ IDEA.
 * User: Olfa
 */
class Locality
{

    String name;
    Date dateCreated
    Date lastUpdated
    Level level
    double longitude
    double latitude
    static belongsTo = [user: User] // the user who manages this locality
    static hasOne = [localityParent: Locality]
    static hasMany = [managers: LocalityManager, children: Locality, addresses: Address]


    static constraints = {
        localityParent nullable: true
        name(nullable: false)
        level(nullable: false)
        user(nullable: true)
        longitude(nullable: true)
        latitude(nullable: true)
        managers nullable: true
        children nullable: true
        addresses nullable: true
    }

    def beforeInsert()
    {
        if (Locality.findById(1) != null)
        {
            if (localityParent == "" || localityParent == null)
            {
                localityParent = Locality.findById(1)
            }

        }
    }

    @Override
    public String toString()
    {
        return name
    }

}
